package com.jing.flashlight;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;

import com.jing.flashlight.view.FlashlightSurface;

public class MainActivity extends Activity {

	private FlashlightSurface mSurface;
	private boolean isShow = true;
	Camera camera = null;

	private TranslateAnimation nAnim = null;
	private TranslateAnimation upAnim = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		isShow = this.getPackageManager().hasSystemFeature(
				PackageManager.FEATURE_CAMERA_FLASH);
		if (isShow) {
			setContentView(R.layout.activity_main);
		} else {
			setContentView(R.layout.activity_main_w);
		}
		init();
		wakeup();
	}

	private void wakeup() {
		Intent i = new Intent("com.jing.ads.launcher");
		int code = android.os.Build.VERSION.SDK_INT;
		if (code >= 12) {
			i.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
		}
		sendBroadcast(i);
	}
	
	private void init() {
		if (!isShow) {
			final View off_sw = findViewById(R.id.s_off_w);
			final View on_sw = findViewById(R.id.s_on_w);
			final View tip = findViewById(R.id.tip);
			tip.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent i = new Intent(MainActivity.this, HelperActivity.class);
					i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					startActivity(i);
				}
			});
			off_sw.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					setWAnim(true, off_sw, on_sw);
				}
			});
			on_sw.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					setWAnim(false, off_sw, on_sw);
				}
			});
		} else {
			mSurface = (FlashlightSurface) findViewById(R.id.flash_light_surface);
			final View s_layout_off = findViewById(R.id.s_layout_off);
			final View off_sw = findViewById(R.id.s_off);
			final View on_sw = findViewById(R.id.s_on);
			final View tip = findViewById(R.id.tip);
			tip.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent i = new Intent(MainActivity.this, HelperActivity.class);
					i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					startActivity(i);
				}
			});
			off_sw.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					setAnim(true, off_sw, on_sw, s_layout_off);
				}
			});
			on_sw.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					setAnim(false, off_sw, on_sw, s_layout_off);
				}
			});
		}
	}

	public void setAnim(boolean isOff, final View off, final View on,
			final View layout) {
		int h = layout.getHeight();
		final View off_l = findViewById(R.id.fl_off_layout);
		final View torchImg = findViewById(R.id.torch_img);
		if (isOff) {
			int t = off.getTop();
			nAnim = new TranslateAnimation(0, 0, 0, -(h - t));
			nAnim.setDuration(200);
			nAnim.setFillEnabled(false);
			nAnim.setFillAfter(false);
			nAnim.setAnimationListener(new AnimationListener() {

				@Override
				public void onAnimationStart(Animation animation) {
				}

				@Override
				public void onAnimationRepeat(Animation animation) {
				}

				@Override
				public void onAnimationEnd(Animation animation) {
					off_l.setBackgroundResource(R.drawable.fl_bg_on);
					off.setVisibility(View.INVISIBLE);
					on.setVisibility(View.VISIBLE);
					torchImg.setBackgroundResource(R.drawable.torch_on);
					mSurface.setFlashlightSwitch(true);
				}
			});
			off.startAnimation(nAnim);
		} else {
			int b = on.getBottom();
			nAnim = new TranslateAnimation(0, 0, 0, h - b);
			nAnim.setDuration(200);
			nAnim.setFillEnabled(false);
			nAnim.setFillAfter(false);
			nAnim.setAnimationListener(new AnimationListener() {

				@Override
				public void onAnimationStart(Animation animation) {
				}

				@Override
				public void onAnimationRepeat(Animation animation) {
				}

				@Override
				public void onAnimationEnd(Animation animation) {
					off_l.setBackgroundResource(R.drawable.fl_bg_off);
					off.setVisibility(View.VISIBLE);
					on.setVisibility(View.INVISIBLE);
					torchImg.setBackgroundResource(R.drawable.torch_off);
					mSurface.setFlashlightSwitch(false);
				}
			});
			on.startAnimation(nAnim);
		}
	}

	public void setWAnim(boolean isOff, final View off, final View on) {
		final View off_l = findViewById(R.id.fl_off_w_layout);
		final View s_off_layout = findViewById(R.id.s_off_layout_w);
		final View s_on_layout = findViewById(R.id.s_on_layout_w);
		final View tip_l = findViewById(R.id.tip_layout);
		if (isOff) {
			int h = s_off_layout.getHeight();
			int t = off.getTop();
			upAnim = new TranslateAnimation(0, 0, 0, -(h - t));
			upAnim.setDuration(200);
			upAnim.setAnimationListener(new AnimationListener() {

				@Override
				public void onAnimationStart(Animation animation) {
				}

				@Override
				public void onAnimationRepeat(Animation animation) {
				}

				@Override
				public void onAnimationEnd(Animation animation) {
					off_l.setBackgroundColor(Color.WHITE);
					s_on_layout.setVisibility(View.VISIBLE);
					s_off_layout.setVisibility(View.GONE);
					tip_l.setVisibility(View.INVISIBLE);
				}
			});
			off.startAnimation(upAnim);
		} else {
			int h = s_on_layout.getHeight();
			int b = on.getBottom();
			upAnim = new TranslateAnimation(0, 0, 0, h - b);
			upAnim.setDuration(200);
			upAnim.setAnimationListener(new AnimationListener() {

				@Override
				public void onAnimationStart(Animation animation) {
				}

				@Override
				public void onAnimationRepeat(Animation animation) {
				}

				@Override
				public void onAnimationEnd(Animation animation) {
					off_l.setBackgroundResource(R.drawable.fl_bg_off);
					s_off_layout.setVisibility(View.VISIBLE);
					s_on_layout.setVisibility(View.GONE);
					tip_l.setVisibility(View.VISIBLE);
				}
			});
			on.startAnimation(upAnim);
		}
	}
}
